<?php

/*
 * Copyright by Sygma Media Inovasi Ω2013
 * 
 * Lead Programmer : Sony K Wibisono,S.Si,M.T.I
 * E-mail : sony@sonywibisono.com
 * 
 * Designer : Mr. Agus
 * 
 */
global $is_login;

function _add(){
    echo "<h2>Add Content</h2>
        <form action=\"?mod=admin&file=contents\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"><input type=\"text\" name=\"id\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Title</td><td class=\"field_object\"><input type=\"text\" name=\"title\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Content</td><td class=\"field_object\">
<textarea name=\"content\" class=\"textbox\" cols='50' rows='5'></textarea> </td></tr>
<tr><td class=\"field_name\">Revision</td><td class=\"field_object\"><input type=\"text\" name=\"revision\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Viwed</td><td class=\"field_object\"><input type=\"text\" name=\"viwed\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function _edit(){
    global $db,$id;
    if(!empty($id)){
        $sql="select * from contents where id=$id";
        $row=$db->fetch_row($sql);
        extract($row);
        echo "<h2>Edit Content</h2>
       <form action=\"?mod=admin&file=contents\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
<input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td class=\"field_name\">Title</td><td class=\"field_object\">
<input type=\"text\" name=\"title\" class=\"textbox\" value=\"$title\" size='50'/> </td></tr>
<tr><td class=\"field_name\">Content</td><td class=\"field_object\">
<textarea name=\"content\" class=\"textbox\" cols='50' rows='5'>$content</textarea> </td></tr>
<tr><td class=\"field_name\">Revision</td><td class=\"field_object\"><input type=\"text\" name=\"revision\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Viwed</td><td class=\"field_object\"><input type=\"text\" name=\"viwed\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
    }
}
function get_max_page(){
    global $db,$pagesize;
    $sql="select count(*) from contents";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return floor($row[0]/$pagesize)+1;
    } else return 0;
}
function _add_process(){
    global $db,$id,$title,$content,$created_on,$updated_on,$revision,$viwed;
    if (!empty($title)){
        $sql="insert into contents values (null,'$title','$content',sysdate(),null,'0','0')";
        $db->query($sql);
        refresh_to("?mod=content", "Back to Content Management", 1);
    }
}
function _edit_process(){
    global $db,$id,$title,$content,$created_on,$updated_on,$revision,$viwed;
    if (!empty($title)){
        $sql="update contents set title='$title',content='$content',
            updated_on=sysdate(),revision=revision+1 where id=$id";
        $db->query($sql);
        refresh_to("?mod=content", "Back to Content Management", 1);
    }
}
function _delete(){
    global $db,$id;
    if (!empty($id)){
        $sql="delete from contents";
        $db->query($sql);
        refresh_to("?mod=content", "Back to Content Management", 1);
    }
}
function _default(){
    global $db,$page,$pagesize,$mod,$file;
    if (empty($page)){
        $start=0;
        $page=0;
    } else {
        $start=$page*$pagesize;
    }
    
    $sql="select * from contents limit $start,$pagesize";
    $result=$db->query($sql);
    $mp=  get_max_page();
    echo "<h2>Content List</h2>";
    echo "<table width='100%' cellspacing=0 cellpadding=2>";
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "<a href=\"?mod=$mod&file=$file&action=add\">Add New</a></td></tr><tr><th>Id</th>
        <th>Title</th><th>Content</th><th>Created On</th><th>Updated On</th>
        <th>Revision</th><th>Viwed</th><th>Action</th></tr>";
    while ($row=$db->fetch_array($result)){
          extract($row);
        echo "<tr><td>$id</td><td>$title</td><td>$content</td><td>$created_on</td>
            <td>$updated_on</td><td>$revision</td><td>$viwed</td><td><a href=\"?mod=$mod&file=$file&action=edit&id=$id\">
        <img class=\"icon_small\" src='img/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\"><img class=\"icon_small\" 
            src='img/delete.png' title='Delete'/></a></td></tr>";
    }
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "</tr>";
    echo "</table>";
}
global $action;
if (!isset($is_login)){
    refresh_to("?mod=admin", "You Must be login first...", 2);
} else {
show_menus('header');
echo "<div class=\"sub_content\">";
switch ($action){
    case 'add':
        _add();
        break;
    case 'edit':
        _edit();
        break;
    case 'add_process':
        _add_process();
        break;
    case 'edit':
        _edit_process();
        break;
    case 'delete':
        _delete();
        break;
    default:
        _default();
        break;
    
}
echo "</div>";
}
?>
